package org.n.shop.service.impl;

import lombok.AllArgsConstructor;
import org.n.shop.config.RedisConfig;
import org.n.shop.domain.Do.SelectAllShoppingCartDo;
import org.n.shop.domain.entity.ShoppingCart;
import org.n.shop.domain.vo.SelectAllShoppingCartCommodityVo;
import org.n.shop.domain.vo.SelectAllShoppingCartStoreVo;
import org.n.shop.mapper.ShoppingCartMapper;
import org.n.shop.result.RestResult;
import org.n.shop.result.RestResultBuilder;
import org.n.shop.service.ShoppingCartService;
import org.n.shop.util.MinioUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {
    private ShoppingCartMapper shoppingCartMapper;
    private RedisTemplate redisTemplate;
    private MinioUtil minioUtil;

    @Override
    public RestResult all(String authorization) {
        int userId = (int) redisTemplate.opsForValue().get(RedisConfig.PREFIX_USER + authorization);

        List<SelectAllShoppingCartDo> selectAllShoppingCartDos = shoppingCartMapper.all(userId);

        for (SelectAllShoppingCartDo selectAllShoppingCartDo : selectAllShoppingCartDos) {
            selectAllShoppingCartDo.setImage(minioUtil.getObject(selectAllShoppingCartDo.getImage()));
        }

        return new RestResultBuilder().success(selectAllShoppingCartDos);

//        Map<Integer, List<SelectAllShoppingCartCommodityVo>> map = new HashMap<>();
//        List<SelectAllShoppingCartDo> selectAllShoppingCartDos = shoppingCartMapper.all(userId);
//        List<SelectAllShoppingCartStoreVo> selectAllShoppingCartStoreVos = new ArrayList<>();
//
//        for (SelectAllShoppingCartDo selectAllShoppingCartDo : selectAllShoppingCartDos) {
//            if (!map.containsKey(selectAllShoppingCartDo.getStoreId())) {
//                SelectAllShoppingCartStoreVo selectAllShoppingCartStoreVo = SelectAllShoppingCartStoreVo
//                        .builder()
//                        .storeId(selectAllShoppingCartDo.getStoreId())
//                        .storeName(selectAllShoppingCartDo.getStoreName())
//                        .list(new ArrayList<>())
//                        .build();
//
//                map.put(selectAllShoppingCartStoreVo.getStoreId(), selectAllShoppingCartStoreVo.getList());
//                selectAllShoppingCartStoreVos.add(selectAllShoppingCartStoreVo);
//            }
//
//            map.get(selectAllShoppingCartDo.getStoreId()).add(
//                    SelectAllShoppingCartCommodityVo
//                            .builder()
//                            .commodityId(selectAllShoppingCartDo.getCommodityId())
//                            .commodityName(selectAllShoppingCartDo.getCommodityName())
//                            .date(selectAllShoppingCartDo.getDate())
//                            .image(selectAllShoppingCartDo.getImage())
//                            .price(selectAllShoppingCartDo.getPrice())
//                            .build()
//            );
//        }
//
//        return new RestResultBuilder().success(
//                selectAllShoppingCartStoreVos
//        );
    }

    @Override
    public RestResult del(int[] shoppingCartIds) {
        if (shoppingCartMapper.del(shoppingCartIds) == shoppingCartIds.length) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail("删除失败");
    }

    @Override
    public RestResult updateNumber(ShoppingCart shoppingCart) {
        if (shoppingCartMapper.updateNumber(shoppingCart) == 1) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail("更新失败");
    }

    @Override
    public RestResult add(String authorization, ShoppingCart shoppingCart) {
        int userId = (int) redisTemplate.opsForValue().get(RedisConfig.PREFIX_USER + authorization);
        shoppingCart.setUserId(userId);
        if (shoppingCartMapper.addNumber(shoppingCart) == 1 || shoppingCartMapper.add(shoppingCart) == 1) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail("加入购物车失败");
    }

    @Override
    public RestResult selectByIds(int[] ids) {
        return new RestResultBuilder().success(
                shoppingCartMapper.selectByIds(ids)
        );
    }

    @Override
    public RestResult size(String authorization) {
        int userId = (int) redisTemplate.opsForValue().get(RedisConfig.PREFIX_USER + authorization);

        return new RestResultBuilder().success(
                shoppingCartMapper.size(userId)
        );
    }
}
